<template>
  <div>
    <router-link
      tag="div"
      to="/"
      class="header-abs"
      v-show="showAbs">
      <div class="iconfont header-abs-back">&#xe8b5;</div>
    </router-link>
    <div class="header-fixed" v-show="!showAbs"
         :style="opacityStyle"
    >
      景区详情
      <router-link to="/">
        <div class="iconfont header-fixed-back">&#xe8b5;</div>
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Header',
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      // documentElement可能不兼容 使用后两个会保证在大多数浏览器上兼容（解决兼容问题）
      let top = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset
      if (top > 0) {
        let opacity = top / 60
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  // activated函数会在重新加载时执行，（首次加载时也执行）
  // activated () { // 将activated换成mounted是因为使用keep-alive时将detail组件排除在外了，所以没有了这两个函数，使用mounted和destroyed代替
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },
  // deactivated () {
  destroyed () {
    // 全局事件的接绑
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style scoped lang="stylus">
  @import '~@styles/varibles.styl'
  .header-abs
    position absolute
    top .2rem
    left .2rem
    width .8rem
    height .8rem
    line-height .8rem
    text-align center
    border-radius .4rem
    background-color rgba(0, 0, 0, .5)
    .header-abs-back
      color #ffffff
      font-size .4rem
  .header-fixed
    position fixed
    top 0
    right 0
    left 0
    overflow hidden
    height: $headerHeight
    line-height $headerHeight
    text-align center
    color #fff
    background $bgColor
    font-size .32rem
    .header-fixed-back
      position absolute
      left 0
      top 0
      width: .64rem
      text-align: center
      font-size: .4rem
      color: #fff
</style>
